<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="id?'编辑月卡':'添加月卡'" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div

        class="form-container"
      >
        <div class="title">车辆信息</div>
        <div

          class="form"
        >
          <!-- 表单校验四元素 -->
          <!-- el-form:  :model :rules -->
          <!-- el-form-item prop -->
          <!-- el-input el-select  v-model -->
          <el-form
            ref="conFromUst"
            label-width="100px"
            :model="cartadd"
            :rules="cartRules"
          >
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="cartadd.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="cartadd.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="cartadd.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="cartadd.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div
        class="form-container"
      >
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form
            ref="conFromMent"
            label-width="100px"
            :model="paymentMent"
            :rules="paymentRules"
          >
            <el-form-item label="有效日期" prop="payTime">
              <div class="block">
                <el-date-picker
                  v-model="paymentMent.payTime"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                />

              </div>
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="paymentMent.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="paymentMent.paymentMethod">
                <el-option
                  v-for="item in payment"
                  :key="item.value"
                  :value="item.value"
                  :label="item.text"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetDelet">重置</el-button>
        <el-button type="primary" @click="conFormAdd">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getAddApi, getHuiXian, getBianJi } from '@/api/card'
export default {
  name: 'AddMothod',
  data() {
    return {
      cartadd: {
        personName: '', // 车主姓名
        phoneNumber: '', // 联系方式
        carNumber: '', // 车牌号码
        carBrand: ''// 车辆品牌
      },
      cartRules: {
        personName: [
          { required: true, message: '车主姓名不能为空', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' },
          // 正则校验
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }

        ],
        carNumber: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' },
          { validator: this.validatorNumber, trigger: blur }
        ],
        carBrand: [
          { required: true, message: '车辆品牌不能为空', trigger: 'blur' }
        ]
      },
      // 支付方式遍历数组
      payment: [
        { text: '支付宝',
          value: 'Alipay' },
        { text: '微信',
          value: 'WeChat' },
        { text: '线下',
          value: 'Cash' }

      ],
      // 表单内容双向绑定;'最新一次月卡缴费信息'

      paymentMent: {
        payTime: [], // 有效日期
        paymentAmount: '', // 支付金额
        paymentMethod: '' // 支付方式
      },
      // '最新一次月卡缴费信息' 校验信息
      paymentRules: {
        payTime: [
          { required: true, message: '有效日期不能为空', trigger: 'blur' }
        ],
        paymentAmount: [
          { required: true, message: '支付金额不能为空', trigger: 'blur' }
        ],
        // 支付方式应为change 更改事件
        paymentMethod: [
          { required: true, message: '支付方式不能为空', trigger: 'change' }
        ]
      }

    }
  },
  // 计算属性用来给id重新定义一个属性
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  // 创建
  created() {
    // 判断如果有id
    if (this.id) {
      // 就执行此回显方法
      this.getCreated()
    }
  },
  methods: {
    // rule  value  callback
    // rule 用不到
    // value:用户输入的数据
    // callback：不管校验成功还是失败都要调用 成功：callback()  失败：callback(new Error(错误信息))
    validatorNumber(rule, value, callback) {
      const valveBale = /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/
      if (valveBale.test(value)) {
        callback()
      } else {
        callback(new Error('车牌号模式不正确'))
      }
    },
    // 点击确定按钮校验方法
    conFormAdd() {
      this.$refs.conFromUst.validate((flag) => {
        if (!flag) return
        this.$refs.conFromMent.validate(async(flag) => {
          if (!flag) return
          console.log('调用接口')

          const requestData = {
            ...this.cartadd,
            ...this.paymentMent,
            cardStartDate: this.paymentMent.payTime[0],
            cardEndDate: this.paymentMent.payTime[1]
          }
          // 因为接口做了校验 多字段不行，所以提交之前把多余的字段删掉
          delete requestData.payTime
          // console.log(requestData)
          // 判断是否有id
          if (this.id) {
            // 如果有id就调用编辑接口
            // 并且提示 ‘月卡编辑成功’
            await getBianJi(requestData)
            this.$message.success('月卡编辑成功')
          } else {
            // 否则就调用添加接口，然后提示‘月卡添加成功’
            await getAddApi(requestData)
            this.$message.success('月卡添加成功')
          }
          this.$router.back()
        })
      })
    },
    // 点击重置按钮重置表单
    resetDelet() {
      // resetFields此方法只对表单中添加prop的样式进行重置，除外只能手动进行表单为空
      this.$refs.conFromUst.resetFields()
      this.$refs.conFromMent.resetFields()
    },
    async  getCreated() {
      const { data } = await getHuiXian(this.id)
      console.log(data)
      const { personName, phoneNumber, carNumber, carBrand, carInfoId
      } = data
      this.cartadd = { personName, phoneNumber, carNumber, carBrand, carInfoId
      }
      const { cardStartDate, cardEndDate, paymentAmount, paymentMethod, rechargeId
      } = data
      this.paymentMent = {
        payTime: [cardStartDate, cardEndDate],
        paymentAmount,
        paymentMethod,
        rechargeId
      }
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview {
      img {
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
